﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class management_Produt_Produt_List : System.Web.UI.Page
{
    private string title = HttpContext.Current.Request.QueryString["title"]; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDrpClass();
            if (Convert.ToInt32(Request.QueryString["cid"]) != 0)
            {
                dropCategory.SelectedValue = Request.QueryString["cid"].ToString();
            }

            Bind(); 

        }
    } 

    //加载数据
    protected void Bind()
    {
        int CId = Convert.ToInt32(dropCategory.SelectedValue); 
        int total = 0;
        BLL.Product.GetPageDT_Where(0, int.MaxValue, CId, out total);
        AspNetPager1.RecordCount = total;
        int StartRecord = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1);
        int MaxRecord = AspNetPager1.PageSize;
        G1.DataSource = BLL.Product.GetPageDT_Where(StartRecord, MaxRecord, CId, out total);
        G1.DataBind(); 
    }
     

    //绑定顶级分类
    private void BindDrpClass()
    {
        int total = 0;
        DataTable dt = BLL.Product_Category.Categorys_Get(0, int.MaxValue, 0, out total); 
        dropCategory.Items.Clear();
        dropCategory.Items.Add(new ListItem("请选择分类", "0"));
        DataRow[] drs = dt.Select("FId= " + 0);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            //顶级分类显示形式
            classname = "╋" + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank = "├─";
            //递归子分类方法
            BindNode(sonparentid, dt, blank);
        }
        dropCategory.DataBind();
    }

    //绑定子分类
    private void BindNode(int parentid, DataTable dt, string blank)
    {
        DataRow[] drs = dt.Select("FId= " + parentid);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            classname = blank + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank2 = blank + "─";
            BindNode(sonparentid, dt, blank2);
        }
    }

    //筛选删除
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < G1.Rows.Count; i++)
        {
            CheckBox Remove = (CheckBox)G1.Rows[i].FindControl("Remove");
            if (Remove.Checked)
            { 
                int id = int.Parse(G1.DataKeys[i].Value.ToString());  
                    Image myimg = (Image)G1.Rows[i].FindControl("myimg");
                    Image myimg1 = (Image)G1.Rows[i].FindControl("myimg1");
                    Image myimg2 = (Image)G1.Rows[i].FindControl("myimg2");
                    if (myimg.ImageUrl != "/userfiles/temp/default.jpg")
                    {
                        Obj.file_delete(myimg.ImageUrl);
                        Obj.file_delete(myimg1.ImageUrl);
                        Obj.file_delete(myimg2.ImageUrl);
                    }
                    BLL.Product.Delete(id); 
            }
        }
        Bind();
    }

    //行内删除
    protected void G1_DeleteCommand(object source, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(G1.DataKeys[e.RowIndex].Value.ToString());
        Image myimg = (Image)G1.Rows[e.RowIndex].FindControl("myimg");
        Image myimg1 = (Image)G1.Rows[e.RowIndex].FindControl("myimg1");
        Image myimg2 = (Image)G1.Rows[e.RowIndex].FindControl("myimg2");
        if (myimg.ImageUrl != "/userfiles/temp/default.jpg")
        {
            Obj.file_delete(myimg.ImageUrl);
            Obj.file_delete(myimg1.ImageUrl);
            Obj.file_delete(myimg2.ImageUrl);
        }
        BLL.Product.Delete(id);
        Bind();
    }

    //pager
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        Bind(); 
    }

    //产品分类改变时重新绑定数据
    protected void dropCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    } 
}
